package com.hfkh.keyun.exception;


import com.hfkh.keyun.common.ApiResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {BusinessException.class})
    public ApiResult handleBusinessException(BusinessException ex){
        return ApiResult.fail(500,ex.getMessage());
    }


    /**
     * 参数校验异常，将校验失败的所有异常组合成一条错误信息
     * @param e 异常
     * @return 异常结果
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ApiResult handleValidException(MethodArgumentNotValidException e) {
        return ApiResult.fail(500,"请求参数校验异常");
    }

    @ExceptionHandler(value = {Exception.class})
    public ApiResult handleSystemException(Exception ex){
        return ApiResult.fail(500,"系统繁忙，请稍后重试！");
    }

}